home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / LineNumberReader.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  218 lines

  1. /*
  2.  * @(#)LineNumberReader.java    1.6 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * A buffered character-input stream that keeps track of line numbers.  A line
  20.  * is considered to be terminated by any one of a line feed ('\n'), a carriage
  21.  * return ('\r'), or a carriage return followed immediately by a linefeed.
  22.  *
  23.  * @version     1.6, 98/07/01
  24.  * @author    Mark Reinhold
  25.  * @since    JDK1.1
  26.  */
  27.  
  28. public class LineNumberReader extends BufferedReader {
  29.  
  30.     /** The current line number */
  31.     private int lineNumber = 0;
  32.  
  33.     /** The line number of the mark, if any */
  34.     private int markedLineNumber;
  35.  
  36.     /** If the next character is a line feed, skip it */
  37.     private boolean skipLF;
  38.  
  39.     /**
  40.      * Create a new line-numbering reader, using the default input-buffer
  41.      * size.
  42.      */
  43.     public LineNumberReader(Reader in) {
  44.     super(in);
  45.     }
  46.  
  47.     /**
  48.      * Create a new line-numbering reader, reading characters into a buffer of
  49.      * the given size.
  50.      */
  51.     public LineNumberReader(Reader in, int sz) {
  52.     super(in, sz);
  53.     }
  54.  
  55.     /**
  56.      * Set the current line number.
  57.      */
  58.     public void setLineNumber(int lineNumber) {
  59.     this.lineNumber = lineNumber;
  60.     }
  61.  
  62.     /**
  63.      * Get the current line number.
  64.      */
  65.     public int getLineNumber() {
  66.     return lineNumber;
  67.     }
  68.  
  69.     /**
  70.      * Read a single character.  Line terminators are compressed into single
  71.      * newline ('\n') characters.
  72.      *
  73.      * @return     The character read, or -1 if the end of the stream has been
  74.      *             reached
  75.      *
  76.      * @exception  IOException  If an I/O error occurs
  77.      */
  78.     public int read() throws IOException {
  79.     synchronized (lock) {
  80.         int c = super.read();
  81.         if (skipLF) {
  82.         if (c == '\n')
  83.             c = super.read();
  84.         skipLF = false;
  85.         }
  86.         switch (c) {
  87.         case '\r':
  88.         skipLF = true;
  89.         case '\n':        /* Fall through */
  90.         lineNumber++;
  91.         return '\n';
  92.         }
  93.         return c;
  94.     }
  95.     }
  96.  
  97.     /**
  98.      * Read characters into a portion of an array.
  99.      *
  100.      * @param      cbuf  Destination buffer
  101.      * @param      off   Offset at which to start storing characters
  102.      * @param      len   Maximum number of characters to read
  103.      *
  104.      * @return     The number of bytes read, or -1 if the end of the stream has
  105.      *             already been reached
  106.      *
  107.      * @exception  IOException  If an I/O error occurs
  108.      */
  109.     public int read(char cbuf[], int off, int len) throws IOException {
  110.     synchronized (lock) {
  111.         int n = super.read(cbuf, off, len);
  112.  
  113.         for (int i = off; i < off + len; i++) {
  114.         int c = cbuf[i];
  115.         if (skipLF) {
  116.             skipLF = false;
  117.             if (c == '\n')
  118.             continue;
  119.         }
  120.         switch (c) {
  121.         case '\r':
  122.             skipLF = true;
  123.         case '\n':    /* Fall through */
  124.             lineNumber++;
  125.             break;
  126.         }
  127.         }
  128.  
  129.         return n;
  130.     }
  131.     }
  132.  
  133.     /**
  134.      * Read a line of text.  A line is considered to be terminated by any one
  135.      * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
  136.      * followed immediately by a linefeed.
  137.      *
  138.      * @return     A String containing the contents of the line, not including
  139.      *             any line-termination characters, or null if the end of the
  140.      *             stream has been reached
  141.      *
  142.      * @exception  IOException  If an I/O error occurs
  143.      */
  144.     public String readLine() throws IOException {
  145.     synchronized (lock) {
  146.         String l = super.readLine();
  147.         if (l != null)
  148.         lineNumber++;
  149.         skipLF = false;
  150.         return l;
  151.     }
  152.     }
  153.  
  154.     /** Maximum skip-buffer size */
  155.     private static final int maxSkipBufferSize = 8192;
  156.  
  157.     /** Skip buffer, null until allocated */
  158.     private char skipBuffer[] = null;
  159.  
  160.     /**
  161.      * Skip characters.
  162.      *
  163.      * @param  n  The number of characters to skip
  164.      *
  165.      * @return    The number of characters actually skipped
  166.      *
  167.      * @exception  IOException  If an I/O error occurs
  168.      */
  169.     public long skip(long n) throws IOException {
  170.     int nn = (int) Math.min(n, maxSkipBufferSize);
  171.     synchronized (lock) {
  172.         if ((skipBuffer == null) || (skipBuffer.length < nn))
  173.         skipBuffer = new char[nn];
  174.         long r = n;
  175.         while (r > 0) {
  176.         int nc = read(skipBuffer, 0, nn);
  177.         if (nc == -1)
  178.             break;
  179.         r -= nc;
  180.         }
  181.         return n - r;
  182.     }
  183.     }
  184.  
  185.     /**
  186.      * Mark the present position in the stream.  Subsequent calls to reset()
  187.      * will attempt to reposition the stream to this point, and will also reset
  188.      * the line number appropriately.
  189.      *
  190.      * @param  readAheadLimit  Limit on the number of characters that may be
  191.      *                         read while still preserving the mark.  After
  192.      *                         reading this many characters, attempting to
  193.      *                         reset the stream may fail.
  194.      *
  195.      * @exception  IOException  If an I/O error occurs
  196.      */
  197.     public void mark(int readAheadLimit) throws IOException {
  198.     synchronized (lock) {
  199.         super.mark(readAheadLimit);
  200.         markedLineNumber = lineNumber;
  201.     }
  202.     }
  203.  
  204.     /**
  205.      * Reset the stream to the most recent mark.
  206.      *
  207.      * @exception  IOException  If the stream has not been marked,
  208.      *                          or if the mark has been invalidated
  209.      */
  210.     public void reset() throws IOException {
  211.     synchronized (lock) {
  212.         super.reset();
  213.         lineNumber = markedLineNumber;
  214.     }
  215.     }
  216.  
  217. }
  218.